<?php
/*
 * BOT remove old schedule pics and they styled copies
 */
mysql_connect("localhost","infopark_com_ua","8888zG6Wtv1k");
mysql_select_db("pro_infopark_com_ua");

$pre_path='/var/www/infopark_com_ua/data/www/pro.infopark.com.ua';
 $folders_list=glob("/var/www/infopark_com_ua/data/www/pro.infopark.com.ua/sites/default/files/styles/*");
 
function delete_styled($filename)
{
    global $folders_list;
    foreach($folders_list as $folder)
    {
        $file=$folder.'/public/'.$filename;
        
        if(file_exists($file))
        {
            unlink($file);
            # print $file."<hr>";
        }
       
    }
    
}  
$sql="SELECT src.field_src_value,src.entity_id
FROM `field_revision_field_schedule_start` start
LEFT JOIN node n ON n.nid=start.entity_id
left join field_data_field_src src ON src.entity_id=n.nid
WHERE (src.field_src_value IS NOT NULL AND src.field_src_value!='') AND 

start.field_schedule_start_value<'".date('Y-m-d H:i:s',strtotime("-1 week"))."'";

$result=mysql_query($sql);
$c=0;
print "<pre>";
while($r=  mysql_fetch_object($result))
{
    #$filename=substr($r->field_src_value,strrpos($r->field_src_value,'/')+1);
    $filename=substr($r->field_src_value,21);
  delete_styled($filename);
 if(file_exists($pre_path.$r->field_src_value))
  {
      unlink($pre_path.$r->field_src_value);
    
  }
    $sql1="DELETE FROM field_data_field_src WHERE entity_id=".$r->entity_id;
    mysql_query($sql1);
    
     $sql2="DELETE FROM field_revision_field_src WHERE entity_id=".$r->entity_id;
    mysql_query($sql2);
    print $filename." deleted\n";
    $c++;
}
print "Total deleted ".$c." files\n\n";
print "</pre>";
?>
